package com.sn.author.monitor.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sn.author.common.context.SpringContext;
import com.sn.author.common.enums.CommentTypeEnum;
import com.sn.author.common.exception.BadRequestException;
import com.sn.author.common.sdk.AbstractResponse;
import com.sn.author.common.sdk.response.IMResponse;
import com.sn.author.domain.entity.ImportAccount;
import com.sn.author.domain.entity.JobAccount;
import com.sn.author.domain.taskEntity.TaskModel;
import com.sn.author.service.impl.DyUserServiceImpl;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;

/**
 * @Author xiaom
 * @Date 2020/10/16 11:40
 * @Version 1.0.0
 * @Description <>
 **/
@Slf4j
public class IMHandler extends AbsHandler {

    public IMHandler(TaskModel taskModel) {
        super(taskModel);
    }

    @Override
    public void execute() throws Exception {
        super.execute();
    }

    @Override
    public IMResponse requestApi() {
        //需要的mapper服务
        DyUserServiceImpl dyUserService = SpringContext.getBean(DyUserServiceImpl.class);
        JobAccount jobAccount = JSON.parseObject(taskModel.getTaskParam().toString(),JobAccount.class);

        //获取账号信息
        ImportAccount account = dyUserService.getDyUserInfo(jobAccount.getAccount());

        IMResponse response;

        CommentTypeEnum typeEnum = CommentTypeEnum.match(jobAccount.getCommentType()).get();
        switch (typeEnum){
            case TEXT_TYPE:
                response = executeTextIM(account,jobAccount);
                break;
            case CARD_TYPE:
                response = executeCardIM(account,jobAccount);
                break;
            default:
                throw new BadRequestException("异常的IM任务类型 type:" + jobAccount.getCommentType());
        }

        log.info("【抖音私信】fromUser:{} toUserIds:{} message:{}",jobAccount.getAccount(),jobAccount.getVideoUrl(),jobAccount.getComment());
        return response;
    }

    private IMResponse executeTextIM(ImportAccount account,JobAccount jobAccount){
        return getSdkService().executeIMSdk(account.getPhone(),account.getPassword(),jobAccount.getComment(),jobAccount.getVideoUrl(),jobAccount.getCommentType(),account.getDeviceId());
    }

    private IMResponse executeCardIM(ImportAccount account,JobAccount jobAccount){
        HashMap<String,String> map = JSON.parseObject(jobAccount.getExecuteParams(), new TypeReference<HashMap<String,String>>(){});
        return getSdkService().executeCardIMSdk(account.getPhone(),account.getPassword(),
                jobAccount.getVideoUrl(),account.getDeviceId(),map.get("title"),map.get("url"),map.get("image"),map.get("desc"));
    }

    @Override
    String getSuccessMessage() {
        JobAccount jobAccount = JSON.parseObject(taskModel.getTaskParam().toString(),JobAccount.class);
        return "账号：" + jobAccount.getAccount() + " 为用户:[" + jobAccount.getVideoUrl() + "] 发送消息成功";
    }

    @Override
    String getErrorMessage(String errmsg) {
        JobAccount jobAccount = JSON.parseObject(taskModel.getTaskParam().toString(),JobAccount.class);
        return "账号：" + jobAccount.getAccount() +  "发送消息失败 错误信息:" + errmsg;
    }

    @Override
    public void responseAfter(AbstractResponse response) {

    }

    @Override
    public void responseErrAfter(String errMsg) {

    }
}
